#ifndef __PHLOGGER_WIN32_LIB_H__
#define __PHLOGGER_WIN32_LIB_H__

#pragma warning(disable: 4996)
#pragma warning(disable: 4995)

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <stdarg.h>
#include <time.h>

#ifndef _vscprintf
	#define _vscprintf(a,b) 4096
#endif //_vscprintf

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif 
#include <Windows.h>
#include "Singleton_T.h"   

#ifndef DISENABLE_LOGGER
  #ifdef _WRITE_LOG_TO_CONSOLE
	inline void OpenLogger(const char*){}
	inline void EnableLogger(bool){}
	inline void SetLoggerPipe(const char*){}
	inline void CloseLoggerPipe(){}    
	inline void SetLogPath(const char*){}
    inline void SetMaxSize(unsigned long) {}
	inline void SetLogFlag(int) {}
	static void WriteLog(int evt,const char* str,...)
	{
        if(!(evt & logConfigs)) return ;
        char stime[32] = "";
        time_t now = time(NULL);
        struct tm *tm = localtime(&(now));
        if (tm) strftime(stime, sizeof(stime),"%Y-%m-%d %H:%M:%S\t", tm);
            
		char *fmtstr = NULL;
		int len = 0;
		va_list   varg;   
		va_start(varg,   str);
		len = _vscprintf( str, varg );
		fmtstr = new char[len+1];
		fmtstr[len] = 0;
		vsnprintf(fmtstr,len, str,   varg);   
		va_end(varg); 
		OutputStringF("%s\t%s\t%s\n",stime, string_event(evt), fmtstr);
		delete [] fmtstr;
	}
  #else	//_WRITE_LOG_TO_CONSOLE
    #include "../../public/oraylog/oraylog.h"
	class CLoggerWrapper {
	public:
		CLoggerWrapper()  { 
            InitInstance(); 
        }

		~CLoggerWrapper() { 
            ExitInstance(); 
        }

		BOOL  WriteLogStringFN(const LOG_FLAG evt,const char* str,...) 
		{
            if(!(evt & logConfigs)) return FALSE;
			char *fmtstr = NULL;
			int len = 0;
			va_list varg;   
			va_start(varg,   str);
			len = _vscprintf( str, varg );
			//_ASSERT(len == 4097);
			fmtstr = new char[len +1];
			fmtstr[len] = 0;
			_vsnprintf(fmtstr, len, str, varg);   
			va_end(varg); 

			if(fmtstr == NULL ) return FALSE;
			BOOL b1 = WriteLogString(evt,fmtstr);
			delete [] fmtstr;
			return b1;
		}

		BOOL SetLogPathAFN(const char* szLogPath) { return SetLogPathA(szLogPath); }

		BOOL GetLogFileNameAFN(char* szFile ,LPDWORD size) { return GetLogFileNameA(szFile,size); }

		BOOL SetLogPrefixAFN(const char *szPrefix) { return SetLogPrefixA(szPrefix); }
	};
    #define  LOGGER_INSTANCE CSingleton_T<CLoggerWrapper>::Instance()

	inline void OpenLogger(const char* name) { LOGGER_INSTANCE.SetLogPrefixAFN(name); }
	inline void SetLogPath(const char* szPath) { LOGGER_INSTANCE.SetLogPathAFN(szPath); }
	inline BOOL GetLoggerFileName(char * buf,LPDWORD size ) { return LOGGER_INSTANCE.GetLogFileNameAFN(buf,size);}
	inline BOOL SetLoggerPipe(const char* pipe) { return TRUE; }
	inline BOOL CloseLoggerPipe() { return TRUE; }
	inline void SetMaxSize(unsigned long size) { return; }
    inline void EnableLogger(bool b1){}

	#define WriteLog LOGGER_INSTANCE.WriteLogStringFN
  #endif //_WRITE_LOG_TO_CONSOLE
#else //DISENABLE_LOGGER

	inline void OpenLogger(const char*){}
	inline void EnableLogger(bool){}
	inline void WriteLog(...){}
	inline void SetLoggerPipe(const char*){}
	inline void CloseLoggerPipe(){}
    inline void SetMaxSize(unsigned long) {}

#endif //DISENABLE_LOGGER

#endif //__PHLOGGER_WIN32_LIB_H__

